// --------------------------------------------------------------------------------------------------------------------
// <copyright file="201203210907434_Init.cs" company="ecblog.codeplex.com">
//   emrecoskun.net - ibrahimatay.com
// </copyright>
// <summary>
//   The initial version of database
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EcBlog.Migrations
{
    using System.Data.Entity.Migrations;

    using EcBlog.Data.Sql;
    using EcBlog.Extensions;

    /// <summary>
    /// The initial version of database
    /// </summary>
    public partial class Init : DbMigration
    {
        /// <summary>
        /// Operations to be performed during the upgrade process.
        /// </summary>
        public override void Up()
        {
            var context = new EcModelContext();
            if (!context.IsTableExists("Comments"))
            {
                this.CreateTable(
                    "Comments",
                    c =>
                    new
                        {
                            CommentId = c.Int(nullable: false, identity: true),
                            UserName = c.String(nullable: false, maxLength: 20),
                            Email = c.String(nullable: false, maxLength: 50),
                            Web = c.String(maxLength: 50),
                            SendResponseMail = c.Boolean(nullable: false),
                            CommentHtml = c.String(nullable: false, maxLength: 4000),
                            PageId = c.Int(nullable: false),
                            IsPublished = c.Boolean(nullable: false), 
                            Created = c.DateTime(nullable: false),
                        }).PrimaryKey(t => t.CommentId).ForeignKey("Pages", t => t.PageId).Index(t => t.PageId);
            }

            if (!context.IsTableExists("Layouts"))
            {
                this.CreateTable(
                    "Layouts",
                    c =>
                    new
                        {
                            LayoutId = c.Int(nullable: false, identity: true),
                            LayoutPath = c.String(nullable: false, maxLength: 500), 
                            Name = c.String(nullable: false, maxLength: 255),
                        }).PrimaryKey(t => t.LayoutId);
            }

            if (!context.IsTableExists("Pages"))
            {
                this.CreateTable(
                    "Pages",
                    c =>
                    new
                        {
                            PageId = c.Int(nullable: false, identity: true),
                            Title = c.String(nullable: false, maxLength: 255),
                            Slug = c.String(nullable: false, maxLength: 255),
                            ContentHtml = c.String(),
                            LayoutId = c.Int(nullable: false),
                            IsCommentable = c.Boolean(nullable: false),
                            IsPublish = c.Boolean(nullable: false), 
                            Published = c.DateTime(nullable: false),
                            Created = c.DateTime(nullable: false),
                            ShowPostList = c.Boolean(nullable: false),
                            Summary = c.String(),
                        }).PrimaryKey(t => t.PageId).ForeignKey("Layouts", t => t.LayoutId).Index(t => t.LayoutId);
            }

            if (!context.IsTableExists("Tags"))
            {
                this.CreateTable(
                    "Tags",
                    c =>
                    new
                        {
                            TagId = c.Int(nullable: false, identity: true),
                            TagName = c.String(nullable: false, maxLength: 50),
                            TagSlug = c.String(nullable: false, maxLength: 50),
                        }).PrimaryKey(t => t.TagId);
            }

            if (!context.IsTableExists("Users"))
            {
                this.CreateTable(
                    "Users",
                    c =>
                    new
                        {
                            UserId = c.Int(nullable: false, identity: true),
                            Username = c.String(nullable: false, maxLength: 4000),
                            FullName = c.String(nullable: false, maxLength: 50),
                            Email = c.String(nullable: false, maxLength: 100),
                        }).PrimaryKey(t => t.UserId);
            }

            if (!context.IsTableExists("SiteSettings"))
            {
                this.CreateTable(
                    "SiteSettings",
                    c =>
                    new
                        {
                            SiteSettingId = c.Int(nullable: false, identity: true),
                            SettingKey = c.String(maxLength: 50),
                            SettingValue = c.String(maxLength: 1000),
                        }).PrimaryKey(t => t.SiteSettingId);
            }

            if (!context.IsTableExists("TagPages"))
            {
                this.CreateTable("TagPages", c => new { Tag_TagId = c.Int(nullable: false), Page_PageId = c.Int(nullable: false), })
                    .PrimaryKey(t => new { t.Tag_TagId, t.Page_PageId }).ForeignKey(
                        "Tags", t => t.Tag_TagId, cascadeDelete: true).ForeignKey(
                            "Pages", t => t.Page_PageId, cascadeDelete: true).Index(t => t.Tag_TagId).Index(
                                t => t.Page_PageId);
            }
        }

        /// <summary>
        /// Operations to be performed during the downgrade process.
        /// </summary>
        public override void Down()
        {
            this.DropIndex("TagPages", new[] { "Page_PageId" });
            this.DropIndex("TagPages", new[] { "Tag_TagId" }); 
            this.DropIndex("Pages", new[] { "LayoutId" });
            this.DropIndex("Comments", new[] { "PageId" });
            this.DropForeignKey("TagPages", "Page_PageId", "Pages");
            this.DropForeignKey("TagPages", "Tag_TagId", "Tags"); 
            this.DropForeignKey("Pages", "LayoutId", "Layouts");
            this.DropForeignKey("Comments", "PageId", "Pages");
            this.DropTable("TagPages");
            this.DropTable("SiteSettings"); 
            this.DropTable("Users");
            this.DropTable("Tags");
            this.DropTable("Pages");
            this.DropTable("Layouts");
            this.DropTable("Comments");
        }
    }
}
